package core.util.mybatis;

import java.lang.reflect.Field;
import java.util.Set;

import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.core.io.FileSystemResource;

public class DiySqlSessionFactoryBean extends SqlSessionFactoryBean{
	
 
	
	public void refresh() {
		new Thread(new Runnable() {
			public void run() {
				
				while (true) {
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					try {
						// 清理已加载的资源标识，方便让它重新加载。
						Field configurationField = SqlSessionFactoryBean.class.getDeclaredField("configuration");
						configurationField.setAccessible(true);
						
						Configuration configuration = (Configuration) configurationField.get(this);
						 
						
						Field loadedResourcesField = Configuration.class.getDeclaredField("loadedResources");
						loadedResourcesField.setAccessible(true);
						
						Set loadedResourcesSet = ((Set) loadedResourcesField.get(configuration));
						loadedResourcesSet.clear();
						System.out.println("asdf" + configuration);
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					
					FileSystemResource[] mapperLocations=new FileSystemResource[2];
					mapperLocations[0] = new FileSystemResource("C:/Users/bj/git/admin/admin/target/classes/data/mybatis/mappings/AddressMapper.xml");
					mapperLocations[1] = new FileSystemResource("C:/Users/bj/git/admin/admin/target/classes/data/mybatis/mappings/AddressMapper2.xml");
					
					setMapperLocations(mapperLocations);


					
					try {
						afterPropertiesSet();
					} catch (Exception e) {
						e.printStackTrace();
					}
					System.out.println("刷新了");
				}
			}
		}).start();
	}
}
